package com.uzykj.modules.blog.service;

import com.uzykj.core.persistence.Page;
import com.uzykj.core.service.CrudService;
import com.uzykj.modules.blog.entity.BlogLabel;
import com.uzykj.modules.blog.mapper.BlogLabelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ghostxbh
 * @date 2020/7/8
 * @description
 */
@Service
@Transactional(readOnly = true)
public class BlogLabelService extends CrudService<BlogLabelMapper, BlogLabel> {
    public BlogLabel get(String id) {
        return super.get(id);
    }

    public List<BlogLabel> findList(BlogLabel blogLabel) {
        return super.findList(blogLabel);
    }

    @Override
    public Page<BlogLabel> findPage(Page<BlogLabel> page, BlogLabel blogLabel) {
        return super.findPage(page, blogLabel);
    }

    @Transactional(readOnly = false)
    public void save(BlogLabel label) {
        super.save(label);
    }

    @Transactional(readOnly = false)
    public void delete(BlogLabel label) {
        super.delete(label);
    }

    @Transactional(readOnly = false)
    public void processLabels(String newLabels, String oldLabels){
        List<String> newLabelList = new ArrayList<>(50);
        List<String> oldLabelList = new ArrayList<>(50);
        if (oldLabels != null) {
            newLabels = newLabels.replaceAll("；", ";");
            oldLabels = oldLabels.replaceAll("；", ";");
            if (oldLabels.contains(";")) {
                String[] split = oldLabels.split(";");
                Arrays.asList(split).forEach(label -> {
                    if (!label.isEmpty()) oldLabelList.add(label);
                });
            } else oldLabelList.add(oldLabels);

            if (newLabels.contains(";")) {
                String[] split = newLabels.split(";");
                Arrays.asList(split).forEach(label -> {
                    if (!label.isEmpty()) newLabelList.add(label);
                });
            } else newLabelList.add(newLabels);

            oldLabelList.forEach(label -> {
                if (newLabelList.contains(label))
                    addLabel(new BlogLabel(label, null, null));
                else delLabel(new BlogLabel(label, null, null));
            });
        } else {
            newLabels = newLabels.replaceAll("；", ";");
            if (newLabels.contains(";")) {
                String[] split = newLabels.split(";");
                Arrays.asList(split).forEach(item -> {
                    if (!item.isEmpty()) newLabelList.add(item);
                });
            } else newLabelList.add(newLabels);

            newLabelList.forEach(label -> addLabel(new BlogLabel(label, null, null)));
        }
    }

    private void addLabel(BlogLabel label){
        BlogLabel blogLabel = mapper.getByName(label.getName());
        if (blogLabel != null) {
            blogLabel.setTotal(blogLabel.getTotal() + 1);
            super.save(blogLabel);
        } else {
            label.setTotal(1);
            label.setSort(30);
            super.save(label);
        }
    }

    private void delLabel(BlogLabel label){
        BlogLabel blogLabel = mapper.getByName(label.getName());
        if (blogLabel != null) {
            blogLabel.setTotal(blogLabel.getTotal() - 1);
            super.save(blogLabel);
        }
    }
}
